<template>
    <tm-app class="app11" bgImg="f5">
        <view
            :style="{ height: systemInfo.statusBarHeight + 'px' }"
            style="background: #fff"
        ></view>

        <view
            class="header"
            :style="{ top: systemInfo.statusBarHeight + 'px' }"
        >
            <image
                src="/static/firends/right.png"
                class="right"
                @tap="goBack"
            ></image>
            <text class="firend">取消管理员</text>
            <view></view>
        </view>

        <scroll-view
            class="content1 d-flex d-c a-center j-center"
            :scroll-y="true"
            :style="{ top: `calc(${systemInfo.statusBarHeight}px + 100rpx)` }"
        >
            <!-- <view class="search_box d-flex j-center">
                <view class="search d-flex a-center">
                    <i
                        class="tmicon tmicon-search"
                        style="color: #ccc; font-size: 30rpx"
                    ></i>
                    <input
                        @input="search"
                        v-model="searchText"
                        type="text"
                        class="inp"
                        placeholder="成员昵称"
                    />
                </view>
            </view> -->

            <view class="nodata" v-show="!cur_groupMembers.length">
                <image
                        class="nodata-img"
                        src="/static/img/nodata.png"
                        alt=""
                    />

                    <view class="text">竟然什么都没有~</view>
            </view>

            <view
                style="width: 710rpx; margin: 0 auto;"
                v-for="(item, index) in cur_groupMembers"
                :key="index"
                @tap="showDialog(item)"
            >
                <view class="success-list">
                    <view class="user-li">
                        <view class="info">
                            <image
                                :saveKey="item.userID"
                                :src="item.avatar || '/static/head.jpg'"
                                class="touxiang"
                            ></image>
                            <!-- <image
                                    src="../../static/firends/svip.png"
                                    class="svip"
                                    v-if="item.isvip"
                                ></image> -->
                        </view>
                        <view class="mes-info">
                            <view class="info-m"><rich-text :nodes="item.nodes"></rich-text></view>
                            <!-- <view class="info-b" v-if="item.type==='group'">
                                    <text class="shenqing">申请加入</text>
                                    <text>{{ item.groupName }}</text>
                                </view> -->

                            <!-- <view class="info-b" v-if="item.type==='people'">
                                    <text class="fs32 bold" style="margin-right:10rpx;">{{ item.nick }}</text>
                                    <text class="shenqing">加你好友</text>
                                </view> -->
                        </view>
                    </view>
                    <!-- <view class="mes-type" @tap="showDialog(item)">转让</view> -->
                </view>
            </view>
        </scroll-view>

        <tm-overlay
            v-model:show="dialogVisible"
            contentAnimation
            style="border-radius: 20rpx"
            @close="close"
        >
            <view @click.stop="" class="sheet">
                <tm-sheet :width="540" style="border-radius: 20rpx; padding: 0">
                    <view style="height: 200rpx">
                        <view class="top">提示</view>
                        <view class="mes">
                            确认将：<span class="mes-name">{{
                                currentItem.name
                            }}</span>设置为普通成员，取消其管理员身份？
                        </view>
                        <!-- <view class="fufei">{{ props.data.groupName}}(已付费¥399)</view> -->
                        <!-- <view class="laiyuan">
                        <text class="from">来自</text>发现-个人主页
                    </view> -->
                        <!-- <view class="info">
                        <text>
                            {{ props.data.nick}}
                        </text>
                    </view> -->
                    </view>
                    <view style="height: 220rpx; margin-top: 80rpx">
                        <view class="btn">
                            <view class="tongyi" @tap="agree">同意</view>
                            <view class="jujue" @tap="dialogVisible=false">取消</view>
                        </view>
                    </view>
                    <!-- <view class=" types">
                        <view class="tongyi">
                            已同意
                        </view>
                        <view class="jujue">已拒绝</view>
                    </view> -->
                </tm-sheet>
            </view>
        </tm-overlay>
    </tm-app>
</template>
<script lang="ts" setup>
import { ref, computed, watch, nextTick } from "vue";
import {
    onReady,
    onShow,
    onHide,
    onLoad,
    onUnload,
    onPageScroll,
} from "@dcloudio/uni-app";
import { systemInfo as systemInfoStore } from "@/store/systemInfo";
import { groupUserUpdateRole } from "@/api/api";
import { imStore } from "@/store/imStore";
import cacheimg from '@/components/cacheimg.vue'

const IM_STORE = imStore();

const goBack = () => {
    uni.navigateBack();
};

const systemInfo: any = systemInfoStore();
const currentItem = ref({});
const dialogVisible = ref(false);
const searchText = ref("");

const showDialog = (item) => {
    currentItem.value = item;
    dialogVisible.value = true;
};

const groupMembers = ref([]);
const cur_groupMembers = ref([]);

const getGroupMembers = async () => {
    const arr = JSON.parse(JSON.stringify(IM_STORE.groupMembers));
    console.log(arr);
    groupMembers.value = arr.filter(item=>item.role === "Admin").map((item) => {
            return {
                ...item,
                name: item.nameCard || item.nick,
            };
        });
    initSearch();
        
};

const search = () => {
    if (!searchText.value) {
        initSearch();
        return;
    }

    cur_groupMembers.value = groupMembers.value.filter((item) => {
        const str = item.name;
        if (str.toLowerCase().search(searchText.value.toLowerCase()) !== -1) {
            return true;
        } else {
            return false;
        }
    });


    matchTextHighLight();
};

const initSearch = () => {
    const clone_groupMembers = JSON.parse(JSON.stringify(groupMembers.value));

    cur_groupMembers.value = clone_groupMembers.map((item) => {
        return {
            ...item,
            nodes: `<div style="color:#555;">${
                item.name
            }</div>`,
        };
    });

};

const matchTextHighLight = () => {
    const reg = new RegExp(searchText.value, "gi");
    cur_groupMembers.value = cur_groupMembers.value.map((item) => {
        const obj = {
            ...item,
        };
        // const str = `<div style="color:#555;">${item.profile.nick}</div>`;

        const str = item.name;

        const nodes = `<div style="color:#555;">${str.replace(reg, (s: any) => {
            return `<span style="color:#23e2a6">${s}</span>`;
        })}</div>`;

        obj.nodes = nodes;

        return obj;
    });

};

let agreeLoading = false;

const agree = async()=>{
    if(agreeLoading){
        return
    }

    uni.showLoading();

    agreeLoading=true;

    const params = {
        groupId:groupId.value,
        userId:currentItem.value.userID,
        role:'Member'
    }



    const res:any = await groupUserUpdateRole(params);
    const { flag } = res;
    uni.hideLoading();
    if(flag!==1){
        uni.hideLoading();
        uni.showToast({
            icon: "none",
            title: "转让失败,请稍后再试",
            duration: 1500,
        });
        return;
    }

    agreeLoading=false;
    dialogVisible.value = false;
    uni.$emit('groupUserRoleUpdate');

    uni.showToast({
            icon: "none",
            title: "取消管理员成功",
            duration: 1500,
    });

    setTimeout(()=>{
        goBack();
    },1500)





}

onShow(() => {
    // setFrienRead();
    // setGroupsRead();
    // uni.$on('SDKReady',getGroupAddList)
});

const groupId = ref("");

onLoad(async (option) => {
    if (option.groupId) {
        groupId.value = option.groupId;
        getGroupMembers();
    }
    /*#ifdef APP-PLUS*/
    //uni.hideTabBar();
    /*#endif*/
    // uni.$on('SDKReady',getGroupAddList)
    // getGroupAddList();
});

onUnload(async () => {
    /*#ifdef APP-PLUS*/
    /*#endif*/
    //uni.$off('SDKReady',getGroupAddList)
});
</script>
<style lang="scss" scoped>
.sheet {
    border-radius: 20rpx;
    overflow: hidden;
    padding:0 40rpx;

    .top {
        width: 100%;
        height: 124rpx;
        background: linear-gradient(0, #ffffff 0%, #baf3da 100%, #cbf7e4 100%);
        font-size: 32rpx;
        font-family: PingFangSC-Medium, PingFang SC;
        font-weight: 600;
        color: #333333;
        text-align: center;
        padding-top: 29rpx;
        border-top-left-radius: 20rpx;
        border-top-right-radius: 20rpx;
    }

    .fufei {
        font-size: 28rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #1dc691;
        text-align: center;
        margin-top: 20rpx;
    }

    .mes {
        font-size: 30rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #999;
        padding:0 40rpx;
        line-height:50rpx;

        .mes-name {
            font-size: 30rpx;
            color: #333333;
            line-height:50rpx;
        }
    }

    .laiyuan {
        font-size: 24rpx;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #666666;
        text-align: center;
        margin: 22rpx 0 0 0;

        .from {
            font-size: 24rpx;
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #999999;
            margin-right: 16rpx;
        }
    }

    .info {
        width: 420rpx;
        background: #f5f5f5;
        border-radius: 10rpx;
        margin: 20rpx auto;
        display: flex;

        flex-wrap: wrap;
        padding: 3rpx;
        box-sizing: border-box;
        padding: 20rpx;
        min-height: 200rpx;

        text {
            font-family: PingFangSC-Regular, PingFang SC;
            font-weight: 400;
            color: #333333;
            font-size: 28rpx;
        }
    }

    .btn {
        width: 460rpx;
        margin: 0 auto;

        .tongyi {
            width: 100%;
            height: 80rpx;
            background: linear-gradient(120deg, #29dba3 0%, #64f1c5 100%);
            border-radius: 20rpx;
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 600;
            color: #ffffff;
            text-align: center;
            line-height: 80rpx;
        }

        .jujue {
            margin-top: 20rpx;
            width: 100%;
            height: 80rpx;
            border-radius: 20rpx;
            border: 2rpx solid #cccccc;
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 600;
            color: #999999;
            text-align: center;
            line-height: 80rpx;
        }
    }

    .types {
        width: 100%;
        height: 120rpx;

        text-align: center;
        display: flex;
        align-items: center;
        justify-content: center;

        .tongyi {
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #1dc691;
        }

        .jujue {
            font-size: 32rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 500;
            color: #999999;
        }
    }
}

.app11 {
    .search_box {
        width: 750rpx;
        background: #fff;
        padding-top: 20rpx;
        padding-bottom: 20rpx;
        margin-bottom: 20rpx;
        .search {
            width: 690rpx;
            height: 60rpx;
            border-radius: 30rpx;
            background: #f5f5f5;
            padding: 0 30rpx;
            .inp {
                width: 570rpx;
                margin-left: 20rpx;
                font-size: 25rpx;
            }
        }
    }

    .header {
        padding: 0 30rpx;
        display: flex;
        height: 100rpx;
        align-items: center;
        justify-content: space-between;
        position: fixed;
        left: 0;
        z-index: 100;
        // position: absolute;
        // top: 0;
        // left: 0;
        width: 750rpx;
        background-color: #ffffff;

        .right {
            width: 48rpx;
            height: 48rpx;
        }

        .firend {
            font-size: 34rpx;
            font-family: PingFangSC-Medium, PingFang SC;
            font-weight: 600;
            color: #000000;
        }
    }

    .content1 {
        position: absolute;
        width: 750rpx;
        left: 0;
        bottom: calc(constant(safe-area-inset-bottom) + 20rpx);
        bottom: calc(env(safe-area-inset-bottom) + 20rpx);
    }

    .content1 {
        .success-list {
            background-color: #ffff;
            width: 710rpx;
            height: 160rpx;
            border-radius: 20rpx;
            display: flex;
            padding: 0 20rpx;
            align-items: center;
            justify-content: space-between;
            position: relative;
            margin-top: 10rpx;

            .mes-type {
                font-size: 28rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #1dc691;
            }

            .mes-type2 {
                font-size: 28rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #999999;
            }

            .mes-type3 {
                font-size: 28rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
            }

            .quanzi {
                width: 70rpx;
                height: 40rpx;
                background: #23e2a6;
                border-radius: 0rpx 20rpx 0rpx 20rpx;
                opacity: 0.9;
                position: absolute;
                top: 0;
                font-size: 24rpx;
                font-family: PingFangSC-Medium, PingFang SC;
                font-weight: 600;
                color: #ffffff;
                text-align: center;
                line-height: 40rpx;
            }

            .user-li {
                display: flex;
                align-items: center;

                .info {
                    position: relative;
                    display: flex;
                    align-items: center;

                    .touxiang {
                        width: 100rpx;
                        height: 100rpx;
                        box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(0, 0, 0, 0.1);
                        border: 3rpx solid #ffffff;
                        border-radius: 50%;
                    }

                    .svip {
                        width: 81rpx;
                        height: 36rpx;
                        position: absolute;
                        bottom: -10rpx;
                        left: 0;
                        right: 0;
                        margin: auto;
                    }
                }

                .mes-info {
                    display: flex;
                    flex-direction: column;

                    margin-left: 16rpx;

                    .info-t {
                        font-size: 32rpx;
                        font-family: PingFangSC-Medium, PingFang SC;
                        font-weight: 600;
                        color: #f19945;
                    }

                    .info-m {
                        font-size: 32rpx;
                        font-family: PingFangSC-Medium, PingFang SC;
                        font-weight: 600;
                        color: #333333;
                    }

                    .info-b {
                        font-size: 28rpx;
                        font-family: PingFangSC-Regular, PingFang SC;
                        font-weight: 400;
                        color: #666666;
                        margin-top: 15rpx;

                        .shenqing {
                            font-size: 28rpx;
                            font-family: PingFangSC-Regular, PingFang SC;
                            font-weight: 400;
                            color: #999999;
                        }
                    }
                }
            }
        }

        .mesage-list {
            margin-top: 10rpx;
            width: 710rpx;
            height: 120rpx;
            background: #ffffff;
            border-radius: 20rpx;
            padding: 0 20rpx;
            display: flex;
            align-items: center;
            justify-content: space-between;

            .user-info {
                display: flex;
                align-items: center;

                .touxiang {
                    width: 100rpx;
                    height: 100rpx;
                    box-shadow: 0rpx 2rpx 6rpx 0rpx rgba(0, 0, 0, 0.1);
                    border: 3rpx solid #ffffff;
                    border-radius: 50%;
                }

                .name {
                    font-size: 32rpx;
                    font-family: PingFangSC-Medium, PingFang SC;
                    font-weight: 600;
                    color: #333333;
                    margin: 0 10rpx 0 16rpx;
                }

                .haoyou {
                    font-size: 28rpx;
                    font-family: PingFangSC-Regular, PingFang SC;
                    font-weight: 400;
                    color: #999999;
                }
            }

            .types {
                font-size: 28rpx;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #999999;
            }
        }
    }
}
</style>    